import { http } from '@/utils/request';
import type { ApiResponse } from '@/utils/request/types';

export interface LoginRequest {
  email: string;
  password: string;
}

export interface LoginUserInfo {
  id: string;
  name: string;
  email: string;
  avatar?: string;
  position?: string;
  phone?: string;
  gender?: number;
  birth?: string;
  status?: number;
  bio?: string;
  role: string;
}

export interface LoginResponse {
  accessToken: string;
  refreshToken: string;
}

export interface UserInfo {
  id: string;
  name: string;
  gender: string;
  avatar: string;
  permTokens: string[];
  menus: Recordable[];
}

// 登录接口
export async function login(data: LoginRequest): Promise<ApiResponse<LoginResponse>> {
  const response = await http.post<LoginResponse>('/api', {
    resource: 'security/auth',
    action: 'login',
    version: 'v1',
    params: {
      principal: data.email,
      credentials: data.password,
      type: 'password',
    },
    meta: {},
  });
  return response;
}

// 登出接口
export async function logout(): Promise<ApiResponse<void>> {
  const response = await http.post('/api', {
    resource: 'security/auth',
    action: 'login',
    version: 'v1',
    params: {},
    meta: {},
  });
  return response;
}

export async function fetchUserInfo(): Promise<ApiResponse<UserInfo>> {
  const response = await http.post<any>('/api', {
    resource: 'security/auth',
    action: 'get_user_info',
    version: 'v1',
    params: {},
    meta: {},
  });
  return response;
}
